package test.excelHttpsample;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kg.apc.jmeter.JMeterPluginsUtils;


/**
 * 当是在gui上创建一个sampler，其调用的顺序是**clearGui()->createTestElement()->modifyTestElement()->configure()**
 * @author Administrator
 *
 */
public class HttpBaseTestSampleGui extends AbstractSamplerGui {
	
	public static final Logger log=LoggerFactory.getLogger(HttpBaseTestSample.class);
	private static final long serialVersionUID=241L;

	private JTextArea logArea;
	private JTextField writea;
	public JTextField getWritea() {
		return writea;
	}

	public JTextField getWritab() {
		return writab;
	}
	private JTextField writab;
	
	private JCheckBox isSelected;
	
	private JButton verifyBody = new JButton("参数校正");//用于校正body参数，当接口参数进行变化的时候，以第一个接口参数为模版，对后续参数进行
	//校正
	
	public HttpBaseTestSampleGui() {
		// TODO Auto-generated constructor stub
		super();
		init();
	}
	

	//创建窗体
	private void init() {
		// TODO Auto-generated method stub
		setLayout(new BorderLayout(0,5));
		setBorder(makeBorder());
		add(createPanel(), BorderLayout.CENTER);
		
	}


	private Component createPanel() {
		final JPanel totalPanel=new VerticalPanel();
		totalPanel.setBorder(makeBorder());
		//先加标题栏
		totalPanel.add(makeTitlePanel());
		//a+b
		JPanel interfaceCheck = new HorizontalPanel();//接口校对栏
		interfaceCheck.setBorder(BorderFactory.createTitledBorder("接口校对"));
		JLabel aLabel=new JLabel("接口校对文件地址:");
		writea=new JTextField(15);
		aLabel.setLabelFor(aLabel);//注意这里
		interfaceCheck.add(aLabel);
		interfaceCheck.add(writea);
		
		verifyBody.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				reviseBody();
			}
			
			
		});
		interfaceCheck.add(verifyBody);
		
		
		JPanel addab=new HorizontalPanel();//接口执行栏，包含调试
		addab.setBorder(BorderFactory.createTitledBorder("接口执行"));//这是标题
		isSelected = new JCheckBox("调试模式", false);
		addab.add(isSelected);
		
		//JLabel jia=new JLabel("+");
		//addab.add(jia);
		
		JLabel bLabel=new JLabel("输入文件名");
		writab=new JTextField(15);
		addab.add(bLabel);
		addab.add(writab);
		
		
		JPanel dateShow =new HorizontalPanel();//接口校对日志回显
		dateShow.setBorder(BorderFactory.createTitledBorder("接口校对日志显示"));
		logArea = new JTextArea(30,30);
		logArea.setLineWrap(true);
		logArea.setWrapStyleWord(true);
		logArea.setColumns(20);
		JScrollPane scroll = new JScrollPane(logArea);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		logArea.setCaretPosition(0);
		dateShow.add(scroll);
		//dateShow.add(logArea);
		logArea.setCaretPosition(logArea.getText().length());
		
		
		
		totalPanel.add(addab);
		totalPanel.add(interfaceCheck);
		totalPanel.add(dateShow);
		return totalPanel;
		
		
		
	}
	
	//设置sample名称
	@Override
	public String getStaticLabel() {
		// TODO Auto-generated method stub
		return JMeterPluginsUtils.prefixLabel("httpBaseTest222222");
	}
	@Override
	public String getLabelResource() {
		// TODO Auto-generated method stub
		return getClass().getSimpleName();
	}
	

	//创建新的sample,并传入到modifyTestElement中
	//似乎是固定的格式
	@Override
	public TestElement createTestElement() {
		// TODO Auto-generated method stub
		HttpBaseTestSample sample=new HttpBaseTestSample();
		modifyTestElement(sample);
		return sample;
	}

	@Override
	public void modifyTestElement(TestElement element) {
		element.clear();
		configureTestElement(element);//这个必写
		//super.configureTestElement(element);//这个必写
		//super.configure(element);
		//practiseDemo01Sample sample=(practiseDemo01Sample) element;
		element.setProperty(HttpBaseTestSample.parentPath, writea.getText());
		element.setProperty(HttpBaseTestSample.fileName, writab.getText());
		element.setProperty(HttpBaseTestSample.isChoose, isSelected.isSelected());
		//sample.setA(writea.getText());
		//sample.setB(writab.getText());
	}
	@Override
	public void clearGui() {
		// TODO Auto-generated method stub
		super.clearGui();
		writab.setText("");
		writea.setText("");
	}
	@Override
	public void configure(TestElement element) {
		// TODO Auto-generated method stub
		super.configure(element);
		writab.setText(element.getPropertyAsString(HttpBaseTestSample.fileName));
		writea.setText(element.getPropertyAsString(HttpBaseTestSample.parentPath));
		isSelected.setSelected(element.getPropertyAsBoolean(HttpBaseTestSample.isChoose));
	}

	
	/**用于校正body参数，当接口参数进行变化的时候，以第一个接口参数为模版，对后续参数进行校正
	 * 此种情况下，会影响到参数缺失的测试用例
	 * 判断是否是同一个接口，以接口路径及方法（post or get）为标志符
	 * 1.分拣各个接口
	 * 2.解析各接口的第一条用例，形成填充模板，json格式，键值对格式，键值+json
	 * 3.解析各接口的其他用例，同时根本情况填充2中的模板
	 * 
	 */
	private void reviseBody() {
		
		logArea.append("开始执行\r\n");
		logArea.append("开始执行\r\n");
		logArea.append("开始执行\r\n");
		logArea.append("开始执行\r\n");
		
		
		
		
		
		
		
	}
	
	
}

